//
//  CellOfNews.m
//  UI12_UITableView自定义Cell
//
//  Created by LGC on 16/3/24.
//  Copyright © 2016年 Apon. All rights reserved.
//

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

#import "CellOfNews.h"
#import "ModelOfNews.h"

@interface CellOfNews ()

// 子控件声明为属性.
@property (nonatomic, retain) UILabel *labelOfTitle;

@property (nonatomic, retain) UILabel *labelOfDate;

@property (nonatomic, retain) UILabel *labelOfSummary;

@property (nonatomic, retain) UIImageView *imageViewOfPic;

@end


@implementation CellOfNews

- (void)dealloc {
    [_labelOfDate release];
    [_labelOfTitle release];
    [_labelOfSummary release];
    [_imageViewOfPic release];
    [super dealloc];
}


#pragma mark - override init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        // 创建子控件.
        
        [self createSubViews];
        
        // cell选中风格.
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    return self;
}

- (void)createSubViews {
    
    self.imageViewOfPic = [[UIImageView alloc]init];
    [self.contentView addSubview:self.imageViewOfPic];

    [_imageViewOfPic release];
    
    self.labelOfTitle = [[UILabel alloc]init];
    [self.contentView addSubview:self.labelOfTitle];

    [_labelOfTitle release];
    
    self.labelOfSummary = [[UILabel alloc]init];
    self.labelOfSummary.numberOfLines = 0;
    [self.contentView addSubview:self.labelOfSummary];

    [_labelOfSummary release];
    
    self.labelOfDate = [[UILabel alloc]init];
    self.labelOfDate.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:self.labelOfDate];

    [_labelOfDate release];
}


#pragma mark - override layoutSubViews

- (void)layoutSubviews {
    // 先创建父类的布局子视图.
    [super layoutSubviews];
    
    // 设置子控件frame属性.
    self.imageViewOfPic.frame = CGRectMake(5, 5 , 85 * 4 / 3.0, 85);
    
    CGFloat height = [CellOfNews heightOfCellNews:self.labelOfSummary.text];
    self.labelOfSummary.frame = CGRectMake(5, 95, WIDTH - 10, height - 85 - 15);
    
    self.labelOfTitle.frame = CGRectMake(85 * 4 / 3.0 + 15, 5, WIDTH - 20 - 85 * 4 / 3.0, 40);
    
    
    self.labelOfDate.frame = CGRectMake(WIDTH - 155, 50, 150, 40);
}

#pragma mark - 子控件赋值.
- (void)setCellContentValues:(ModelOfNews *)model {
    
    // 子控件赋值.
    self.imageViewOfPic.image = [UIImage imageNamed:@"h1.jpeg"];
    
    self.labelOfTitle.text = model.title;
    
    
    self.labelOfDate.text = [self dateFormatReverseWithTimeInterval:model.lastUpdateTime];
    
    self.labelOfSummary.text = model.summary;
    
}

/** 时间格式的转换. 核心API, NSDate*/
- (NSString *)dateFormatReverseWithTimeInterval:(NSString *)timeInterval {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
  
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval.doubleValue];
 
    NSString *string = [dateFormatter stringFromDate:date];
    
    return string;
}

#pragma mark - 计算高度.
/** 提供计算cell高度的API. */
+(CGFloat)heightOfCellNews:(NSString *)text {
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 10;
    // 计算新的矩形.
    // 参数1: 预估大小.
    // 参数2: 预估选项.
    // 参数3: 文字属性.
    // 参数4: 绘制上下文.
    CGRect newRect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    
    return newRect.size.height + 100;

}

#pragma mark - other.
// 从图形化中苏醒
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



@end
